import { Toast } from 'antd-mobile'
import { useEffect, useRef, useState } from 'react'

import { commonApi } from '@/apis'
import { WelcomeRefProps } from '@/components'

export function useInit() {
  const welcomeRef = useRef<WelcomeRefProps>(null)
  const [title, setTitle] = useState('-')

  function handleSay() {
    welcomeRef.current?.say()
  }

  async function init() {
    const loading = Toast.show({
      icon: 'loading',
      content: '加载中',
      duration: 0
    })

    try {
      const res = await commonApi.getInfo()

      setTitle(res.title)
    } finally {
      loading.close()
    }
  }

  useEffect(() => {
    init()
  }, [])

  return {
    welcomeRef,
    title,
    handleSay
  }
}
